import {Component, ViewEncapsulation, Input, Output, EventEmitter} from '@angular/core';
import {IdeaService} from '../../../services/app.idea';
import {BaThemeSpinner}  from '../../../theme/services';
import {UserService} from '../../../services/app.user';
declare let toastr:any;
declare let $:any;

@Component({
    selector: 'abuse',
    encapsulation: ViewEncapsulation.None,
    styleUrls: ['./abuse.scss'],
    templateUrl: './abuse.html'
})
export class Abuse {
    @Input()
    idea:any;
    @Output()
    notifyAbuseModal:EventEmitter<any> = new EventEmitter();

    public radioItems:any;
    public abusButton:any;
    public abuseText:any;
    public reportAbused:any;
    public submitButton:any;
    public otherOption:any;

    constructor(private ideaService:IdeaService, private spinner:BaThemeSpinner, public userService:UserService) {
        this.setOptionForRadio();
    }

    // For setting the values at radio buttons
    public setOptionForRadio() {
        let item:string|string|string|string[];
        item = ['It is annoying or not interesting.', 'It contains false Information.', 'It is spam.', 'Other.'];
        this.radioItems = item;
        this.abusButton = true;
    }

    // To enable and disable the submit button
    public abuseButton(item) {
        this.abusButton = false;
        this.abuseText = true;
        if (item === 'Other.') {
            this.abusButton = true;
            this.abuseText = false;
        }
    }

    // To  get the value of category clicked by the user
    public reportAbuseList(data) {
        this.reportAbused = data;
    }

    // To show or hide text area
    public showTextArea(value) {
        this.abusButton = !(value !== 'undefined' && value.trim().length);
    }

    // To  report the idea as abusive
    public reportAbuse(idea):void {
        this.spinner.showProgress();
        this.submitButton = false;
        let data = {
            ideaId: this.idea._id,
            category: this.reportAbused,
            reason: this.otherOption
        };
        this.ideaService.reportAbuse(data).subscribe(
            data => this.reportAbuseSucessfully(idea),
            error => this.reportAbuseFail(error));
    }

    // To close modal and set value after success
    private reportAbuseSucessfully(idea) {
        this.spinner.hideProgress();
        idea.isAbused = true;
        this.notifyAbuseModal.emit('close');
        this.otherOption = '';
        toastr.success('Idea reported successfully');
    }

    // To close modal after failure
    private reportAbuseFail(error) {
        this.spinner.hideProgress();
        this.notifyAbuseModal.emit('close');
        toastr.error(error.error_message);
    }

    // Function for user log keystorke event API call
    public onKeyPressAbuseModal(event) {
        let input = {
            'target': 'Input ' + event.code + ' abuse report',
            'value': 'abuse report',
            'action': 'keystroke'
        };
        this.userService.setUserLogs(input).subscribe(
            data => this.logSuccess(data),
            error => {
            });
    }

    // Function for user log got success
    public logSuccess(data) {
    }

}
